﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DispatchAssist.BusinessLogical.Model;
using DispatchAssist.BusinessLogical.DAL;

namespace DispatchAssist.BusinessLogical.BLL
{
    /// <summary>
    /// 机场信息、机场三字码、四字码转换工具
    /// </summary>
    public class InfoAirportHelper
    {
        static IDataProvider provider = new SqlDataProvider();
        static List<InfoAirport> infolist = LoadInfoList();
        static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private static List<InfoAirport> LoadInfoList()
        {
            try
            {
                return provider.LoadInfoAirportList();
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex);
                return null;
            }
        }

        /// <summary>
        /// 根据机场四字码获取三字码
        /// </summary>
        /// <param name="code4"></param>
        /// <returns></returns>
        public static string GetApt3Code(string code4)
        {
            var apt = infolist.Find(a => a.Code4 == code4);
            if (apt != null)
            {
                return apt.Code3;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 根据机场三字码获取四字码
        /// </summary>
        /// <param name="code3"></param>
        /// <returns></returns>
        public static string GetApt4Code(string code3)
        {
            var apt = infolist.Find(a => a.Code3 == code3);
            if (apt != null)
            {
                return apt.Code4;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 根据机场三字码或四字码获取机场实体
        /// </summary>
        /// <param name="code3"></param>
        /// <returns></returns>
        public static InfoAirport GetIntoAirport(string code)
        {
            if (code.Length == 3)
            {
                return infolist.Find(a => a.Code3 == code);
            }
            else
            {
                return infolist.Find(a => a.Code4 == code);
            }
        }

        public static List<InfoAirport> GetAllAirports()
        {
            return infolist;
        }
    }
}
